//
//  UploadLicenseViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/6/30.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "UploadLicenseViewController.h"

@interface UploadLicenseViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,weak) IBOutlet UIButton *addFileBtn;
@property (nonatomic,weak) IBOutlet UIView *fileNameLab;

@property (strong, nonatomic) UIImagePickerController *licenseImagePC;
@property (weak, nonatomic) IBOutlet UIImageView *licenseImageV;
@property (nonatomic, strong) NSData *licenseImageData;

@property (nonatomic,weak) IBOutlet UILabel *noImageLab;
@property (nonatomic,weak) IBOutlet UILabel *tipLab;
@end

@implementation UploadLicenseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"上传营业执照"];
    self.fileNameLab.layer.borderColor = [UIColor colorWithRed:239/255.0f green:239/255.0f blue:236/255.0f alpha:1].CGColor;
    self.fileNameLab.layer.borderWidth = 0.5f;
    self.addFileBtn.layer.cornerRadius = 50/2;
    self.addFileBtn.layer.masksToBounds = YES;
    
    [self.addFileBtn addTarget:self action:@selector(getIdImage) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"url=%@",self.licenseImageUrl);
    if (self.licenseImageUrl){
        UIImage *imgFromUrl=[[UIImage alloc]initWithContentsOfFile:self.licenseImageUrl];
        [self.licenseImageV setImage:imgFromUrl];
        [self.tipLab setHidden:YES];
        [self.noImageLab setHidden:YES];
    }
}
#pragma mark - custom method
-(void)getIdImage
{
    self.licenseImagePC = [[UIImagePickerController alloc] init];
    [self.licenseImagePC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    self.licenseImagePC.delegate = self;
    [self presentViewController:self.licenseImagePC animated:YES completion:nil];
    
}

- (NSData *)dataWithImage:(UIImage *)image {
    NSData* imageData=UIImageJPEGRepresentation(image, 0.6);
    if (imageData == nil) {
        return UIImagePNGRepresentation(image);
    }
    return imageData;
}
#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    if (picker == self.licenseImagePC) {
        UIImage *image  = [info objectForKey:UIImagePickerControllerOriginalImage];//UIImagePickerControllerEditedImage
        
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *documentsDirectory=[paths objectAtIndex:0];
        NSString *savedImagePath=[documentsDirectory stringByAppendingPathComponent:@"license.jpg"];
        [[self dataWithImage:image] writeToFile:savedImagePath atomically:YES];
        self.licenseImageUrl = savedImagePath;
        
        self.licenseImageData = [self dataWithImage:image];
        [self.licenseImageV setImage:image];
        
        [self.noImageLab setHidden:YES];
        [self.tipLab setHidden:YES];
        self.uploadeSuccess(self.licenseImageData,self.licenseImageUrl);
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end
